import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QHBoxLayout, QWidget

class CloseWindow(QMainWindow):
    def __init__(self, parent = None):
        super().__init__(parent)

        self.resize(400,400)
        
        self.initUI()
    def initUI(self):
        self.centerWindget = QWidget()#设置中央窗口
        
        hLaout = QHBoxLayout()#创建一个水平布局对象
        self.closeButton = QPushButton("关闭窗口")#创建一个关闭按钮
        # self.closeButton.move(200, 200)#设置按钮
        hLaout.addWidget(self.closeButton)#将关闭按钮添加到水平布局中
        
        self.centerWindget.setLayout(hLaout)#将布局放置在容器中,将布局设置给中央部件
        self.setCentralWidget(self.centerWindget)#将中央部件设置给主窗口
        
        self.closeButton.clicked.connect(self.closeWindow)
    def closeWindow(self):
        """ 两中关闭方法都可以 """
        # self.close()
        APP = QApplication.instance()
        APP.quit()
        
def main():
    app = QApplication(sys.argv)
    w = CloseWindow()
    w.show()
    sys.exit(app.exec_())
    
if __name__ == "__main__":
    main()